<?php
    class NoteMapper {
        private
            $responses = array();
        const
            MYSQL_NO_CONNECTED = 'Error connected to database.',
            MYSQL_DB_NO_SELECTED = 'Error select database.',
            MYSQL_QUERY_ERROR = 'Error query.',
            MYSQL_CONNECTED = 'Connected to database ok.',
            MYSQL_DB_SELECTED = 'Selected database ok.',
            NOTE_INSERTED = 'Note inserted.',
            NOTE_DELETED = 'Note deleted.',
            NOTE_UPDATED = 'Note update.',
            NOTE_EXISTS = 'Note exists.',
            NOTE_NO_EXISTS = 'Note no exists.',
            NOTES_NO_EXISTS = 'Notes no exists.',
            NOTES_SELECTED = 'Notes selected.';
        function __construct(){
            $varsConnect = array(
                'host' => 'localhost',
                'login' => 'root',
                'password' => '',
                'name' => 'embria3'
            );
            mysql_connect(
                $varsConnect['host'],
                $varsConnect['login'],
                $varsConnect['password']
            );
            mysql_select_db($varsConnect['name']);
            mysql_query('set character_set_client="utf-8"');
            mysql_query('set character_set_results="utf-8"');
        }
        function getResponses(){
            return $this->responses;
        }
        function insert($note){
            $query = mysql_query("SELECT * FROM notes WHERE
                text='" . $note->text . "'
            ");
            $row = mysql_fetch_assoc($query);
            if (empty($row)){
                $query = mysql_query("INSERT INTO notes VALUES(
                    NULL,
                    '" . $note->userId . "',
                    '" . $note->text . "'
                )");
                if ($query){
                    $this->responses[] = self::NOTE_INSERTED;
                } else {
                    $this->responses[] = self::MYSQL_QUERY_ERROR;
                }
            } else {
                $this->responses[] = self::NOTE_EXISTS;
            }
        }
        function delete($note){
            $query = mysql_query("DELETE FROM notes WHERE
                id='" . $note->id . "' AND
                userId='" . $note->userId . "'
            ");
            if ($query){
                $this->responses[] = self::NOTE_DELETED;
            } else {
                $this->responses[] = self::MYSQL_QUERY_ERROR;
            }
        }
        function update($note){
            $query = mysql_query("UPDATE notes SET
                text='" . $note->text . "' WHERE
                id='" . $note->id . "' AND
                userId='" . $note->userId . "'
            ");
            if($query){
                $this->responses[] = self::NOTE_UPDATED;
            } else {
                $this->responses[] = self::MYSQL_QUERY_ERROR;
            }
        }
        function selectNotes(){
            $query = mysql_query("SELECT * FROM notes");
            $rows = array();
            while ($row = mysql_fetch_assoc($query)){
                $rows[] = $row;
            }
            return $rows;
        }
    }
?>